package com.jhhc.BDMS.Data.Service;

import com.jhhc.BDMS.Data.Pojo.MonitorInfo;
import com.jhhc.BDMS.Data.Pojo.SensorType;
import com.jhhc.BDMS.Data.Pojo.Station;
import com.jhhc.FloodForecast.Sensor.Pojo.*;
import com.jhhc.FloodForecast.Sensor.Service.*;
import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.RHDB.Data.Service.ST_TIDE_RService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class GetStayionService {

    @Autowired
    private BDMS_ST_STBPRP_BService bdms_st_stbprp_bService;
    @Autowired
    private BDMS_ST_SENSOR_BService bdms_st_sensor_bService;
    @Autowired
    private BDMS_ST_SENSORALARM_BService bdms_st_sensoralarm_bService;
    @Autowired
    private BDMS_ST_SENSORTYPE_BService bdms_st_sensortype_bService;
    @Autowired
    private BDMS_ST_MONITORITEM_BService bdms_st_monitoritem_bService;
    @Autowired
    private ST_TIDE_RService st_tide_rService;


    //获取站名站码
    public List<Station> getStation(Integer projectId){
        List<Station> stations = new ArrayList<>();
        //根据项目id获取站名站码
        List<BDMS_ST_STBPRP_B> bdms_st_stbprp_bs =  bdms_st_stbprp_bService.selectStationByProject(projectId);
        List<String> stationCodes = new ArrayList<>();
        for (BDMS_ST_STBPRP_B bdms_st_stbprp_b:bdms_st_stbprp_bs) {
            Station stationInfo = new Station();
            stationCodes.add(bdms_st_stbprp_b.getStcd());
        }
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectBySTCD29(projectId,stationCodes);
        //获取告警信息
        List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs = bdms_st_sensoralarm_bService.selectByStcd(stationCodes,"TDZ",projectId);
        //获取历史最高潮位
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:bdms_st_sensor_bs){
            Station stationInfo = new Station();
            stationInfo.setStationName(bdms_st_sensor_b.getStnm());
            stationInfo.setStationCode(bdms_st_sensor_b.getStcd());
            BDMS_ST_SENSORALARM_B bdms_st_sensoralarm_b = bdms_st_sensoralarm_bs.stream().filter(BDMS_ST_SENSORALARM_B->BDMS_ST_SENSORALARM_B.getStcd().equals(bdms_st_sensor_b.getStcd())).findAny().orElse(null);
            if(bdms_st_sensoralarm_b!=null){
                stationInfo.setWarningTdz(String.valueOf(bdms_st_sensoralarm_b.getAlarmvalue()));
            }

            ST_TIDE_R max = st_tide_rService.selectMAXData(bdms_st_sensor_b.getDatacode());
            if (max!=null) {
                stationInfo.setAll_TimeHighTdz(String.valueOf(max.getTdz()));
            }
            stations.add(stationInfo);
        }
            return  stations;
    }

    //获取要素信息
    public List<MonitorInfo>  getMonitorItem(Integer projectId,String stationCode){
        List<MonitorInfo> monitorInfos = new ArrayList<>();
        List<BDMS_ST_MONITORITEM_B> bdms_st_monitoritem_bs =  bdms_st_monitoritem_bService.seletcMonitorInfo(projectId,stationCode);

        for (BDMS_ST_MONITORITEM_B bdms_st_monitoritem_b:bdms_st_monitoritem_bs){
            MonitorInfo monitorInfo = new MonitorInfo();
            monitorInfo.setMonitorName(bdms_st_monitoritem_b.getMonitornm());
            monitorInfo.setMonitorAbb(bdms_st_monitoritem_b.getMonitorabb());
            monitorInfo.setMonitorType(bdms_st_monitoritem_b.getMonitortp());


            monitorInfos.add(monitorInfo);
        }
        return monitorInfos;
    }

    //获取传感器信息
    public List<SensorType>  getSensorType(Integer monitorType){
        List<SensorType> sensorTypes =new ArrayList<>();
        List<BDMS_ST_SENSORTYPE_B>  bdms_st_sensortype_bs = bdms_st_sensortype_bService.selectSensortps(monitorType);
        bdms_st_sensortype_bs.sort(Comparator.comparing(BDMS_ST_SENSORTYPE_B::getSort));
        for (BDMS_ST_SENSORTYPE_B bdms_st_sensortype_b:bdms_st_sensortype_bs){
            SensorType sensorType = new SensorType();
            String name = bdms_st_sensortype_b.getSersornm();
            String sensortype = bdms_st_sensortype_b.getSensortp();
            String sensorUnit =bdms_st_sensortype_b.getUnit();
            String range =bdms_st_sensortype_b.getRange();
            String [] ran =range.trim().split(",");
            Integer minValue = Integer.valueOf(ran[0].trim());
            Integer maxValue = Integer.valueOf(ran[1].trim());
            sensorType.setName(name);
            sensorType.setSensorType(sensortype);
            sensorType.setSensorUnit(sensorUnit);
            sensorType.setMinValue(minValue);
            sensorType.setMaxValue(maxValue);
            sensorTypes.add(sensorType);
        }
        return sensorTypes;
    }


}
